

if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}

auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function 看视频() {
  sleep(1000)
  var 同意 = text("同意").findOne(1000)
  if(同意){同意.click()}
  var 首页 = text('首页').findOne(30000)
  if (首页) {
    sleep(time)
    var 活动 = text("活动").findOne(5000)
    if (活动) {
      活动.parent().click()
      sleep(5000)
      checkLog();
      var 弹窗 = text("确认").findOne(3000)
      if (弹窗) {
        弹窗.click()
        toastLog('您已完成观看视频任务,开始下一个任务')
        sleep(time)
      } else {
        var index = 1
        while (index < 6) {
          var random_time = 60000;
          toastLog("观看CHMV第" + index + "个视频中...")
          sleep(time)
          toastLog("预计时长" + (random_time / 1000) + "秒~")
          sleep(random_time)
          swip_up()
          index++
          utils.updateAppProgress(index*20);
          var 弹窗 = text('确认').findOne(1000)
          if (弹窗) {
            弹窗.click()
            toastLog('您已完成观看视频任务,开始下一个任务')
            sleep(time)
            break ;
          }
        }
      }
    }
  } else {
    toastLog('进入首页失败，即将退出')
    sleep(time)
  }
}


function checkLog(){
 var login =  id("login_ok").findOne(5000);
 if(login){
    login.click();
    toastLog("登录中")
    sleep(5000);
    var 首页 = text('首页').findOne(30000)
    if(首页){
      首页.click();
      sleep(2000)
      var 推荐 = text("推荐").findOne(5000)
      if (推荐) {
        推荐.parent().click()
      }
      sleep(2000)
      var 活动 = text("活动").findOne(5000)
      if (活动) {
        活动.parent().click()
      }
      log("首页.. 推荐 .. 活动")
    }
 }

}


function task2(){
  var all = id("first_all").findOne(1000);
  all.click();
  sleep(1000)
  var 我的任务 = text("我的任务").findOne(1000);
  if(我的任务){
    我的任务.click();
    sleep(1000)
    //先签到
    var 立即签到 = text("立即签到").findOne(3000);
    if(立即签到){
      log("进行签到");
      立即签到.click();
      sleep(500);
    }
    id("daywork_list").findOne().children().forEach(element => {
      var target = element.findOne(id("everywork_type"));
      if(!target){return ;}
      var text = target.text();
      if(text == "可领取"){
        target.click();
        log("完成积分领取")
      }
      if(text == "未完成" || text == "去点赞"){
        target.click();
        log("开始推荐视频点赞")
        sleep(3000);
        //点赞10个视频
        for(var i = 0;i<15;i++){
          var 点赞 = id("item_videos_heart").findOne(1000);
          if(点赞){
            点赞.click();
            log("完成[" + i +"] 个视频点赞.")
            sleep(random(1000,3000))
            swip_up();
            sleep(random(2000,5000))
          }
        }
      }
      sleep(2000);
    });
  }
 
}


function main() {
  var open_app = launchAppEnhance("CHMV")
  if (open_app) {
    看视频()
    task2()
    task2() //任务点赞之后 需要去领取
  } else {
    toastLog("CHMV打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main()
  utils.updateAppProgress(100);
} catch (error) {
  toast(error)
}


function bezier_curves(cp, t) {
  cx = 3.0 * (cp[1].x - cp[0].x);
  bx = 3.0 * (cp[2].x - cp[1].x) - cx;
  ax = cp[3].x - cp[0].x - cx - bx;
  cy = 3.0 * (cp[1].y - cp[0].y);
  by = 3.0 * (cp[2].y - cp[1].y) - cy;
  ay = cp[3].y - cp[0].y - cy - by;

  tSquared = t * t;
  tCubed = tSquared * t;
  result = { "x": 0, "y": 0 };
  result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
  result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
  return result;
};

function sml_move(qx, qy, zx, zy, time) {
  //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
  var xxy = [time];
  var point = [
    { "x": qx, "y": qy },
    { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
    { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
    { "x": zx, "y": zy }
  ];
  for (let i = 0; i < 1; i += 0.08) {
    xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
    xxy.push(xxyy);
  }
  gesture.apply(null, xxy);
}

function swip_up() {
  var w = device.width
  var h = device.height
  sml_move(w / 2, h * 0.8 + h * random() * 0.1, w / 2, h * 0.15, 200 + 600 * random());
}

storage.put("script_running_status", "end");
